use cmsis_rtos2_rs::{os_delay, static_mem::{Task, Thread}};
use proto_bytes::{frame::{Cmd, Proto}, package::ty};

use crate::{address::{IPC, MY}, cmd, conn};





struct StatusTask;
static THREAD: Thread<1024> = Thread::uninit();

impl Task for StatusTask {

    fn run(&self) {
        let mut seq = 0;
        loop {
            #[cfg(feature = "cp_cargo")]
            let body = crate::cp_cargo::status::collect();

            #[cfg(feature = "cp_drop")]
            let body = crate::cp_drop::status::collect();
            
            #[cfg(feature = "cp_door")]
            let body = crate::cp_door::status::collect();

            #[cfg(feature = "cp_heat")]
            let body = crate::cp_heat::status::collect();

            #[cfg(feature = "cp_weight")]
            let body = crate::cp_weight::status::collect();

            conn::write(Proto {
                ty: ty::NOTIFY,
                seq,
                src: MY,
                dst: IPC,
                body: Cmd {
                    cmd: cmd::STATUS,
                    body
                }
            });
            
            #[cfg(feature = "cp_weight")]
            conn::write(Proto {
                ty: ty::NOTIFY,
                seq,
                src: MY,
                dst: crate::address::HEAT,
                body: Cmd {
                    cmd: cmd::STATUS,
                    body
                }
            });

            seq += 1;
            os_delay(500);
        }        
    }
}



pub unsafe fn start() {
    THREAD.spawn(&StatusTask);
}







